package jp.ac.utokyo.pinyomi;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class AbstractTable {
	public HashMap<String, Pair> map=null;
	
	public AbstractTable () throws IOException{
		preSet();
		setMap(readfile());
	}
	
	public void preSet(){}
	
	/**
	 * readin the data file and genarate a hashmap 
	 * @throws IOException
	 */
	protected abstract HashMap<String, Pair> readfile() throws IOException;

	public HashMap<String, Pair> getMap() {
		return map;
	}

	public void setMap(HashMap<String, Pair> m) {
		map = m;
	}
	
	/**
	 * Do normalization on the weight(val) in each Pair inside AbstractMap
	 */
	public void normalize () {
		Set<Map.Entry<String, Pair>> s = map.entrySet();
		for(Map.Entry<String, Pair> me: s){
			me.getValue().normalize();
		}
	}

	/**
	 * @param k The key Character<BR>
	 * a Chinese Character for the "Chinese lookup system" or<BR>
	 * a Japanese Character for the "Japanese lookup system"
	 * @return Pair object, which contains two arraylist by pair.
	 */
	public Pair getMapVal(String k){
		return map.get(k);
	}
	
	/**
	 * 
	 * @param k the key character of the map
	 * @param pk the key character of the Pair
	 * @return the log-probility
	 */
	public double getLogProb(String k, String pk){
		Pair p = map.get(k);
		if (p==null) return Settings.nullLogProb;
		return p.getLogProb(pk);
	}
	
	public double getProb(String k, String pk){
		return Math.exp(getLogProb(k, pk));
	}
	
	public boolean hasRoute(String k, String pk){
		Pair p = map.get(k);
		if (p==null) return false;
		return p.haskey(pk);
	}
	
}
